<?php
// archivos necesarios
require_once('./php_lib/conexion.php');


switch($_GET['funcion']) {
	
	case 1: // INSERT
	    $query = sprintf("INSERT INTO Sucursal ( suc_des, suc_calle, loc_id) VALUES ('%s', '%s', '%s')",
                       $_POST['suc_des'],
                       $_POST['suc_calle'],
                       $_POST['loc_id']);
  
		if( mysql_query($query, $db_zapatos) == TRUE) 
			$jsondata['respuesta'] = 1;
		else 
			$error['insert'] = mysql_error();
		
		break;
	case 2: // UPDATE
		$suc_id = $_GET['suc_id'];
		// Inserto los datos de registro en la db
		$query = sprintf("UPDATE Sucursal SET suc_des = '%s', suc_calle = '%s', loc_id = '%s' WHERE suc_id = $suc_id;",
				$_POST['suc_des'],
				$_POST['suc_calle'],
				$_POST['loc_id'],
				$_POST['suc_id']);
				
		if( mysql_query($query, $db_zapatos) == FALSE) 
			$error['update'] = mysql_error();			//$error['update'] = "Ocurrio un error al intertar guardar los cambios.";
		else 
			$jsondata['respuesta'] = 1;
		break;
	case 3: // DELETE
		$suc_id = $_GET['suc_id'];
		mysql_query("DELETE FROM Bodega, Sucursal,Sector, Pro_Sec, Bod_Per 
				USING Sucursal
				INNER JOIN Bodega  USING (suc_id)
				INNER JOIN Sector  USING (bod_id)
				INNER JOIN Pro_Sec USING (sec_id)
				INNER JOIN Bod_Per USING (bod_id)
				WHERE (sucursal.suc_id = $suc_id   and 
				       sector.bod_id=bodega.bod_id and 
					   Pro_Sec.sec_id=Sector.sec_id and 
					   Bod_Per.bod_id=Bodega.bod_id);"						
						, $db_zapatos);
	
	
		mysql_query("DELETE FROM Bodega, Sucursal,Sector, Pro_Sec
				USING Sucursal
				INNER JOIN Bodega  USING (suc_id)
				INNER JOIN Sector  USING (bod_id)
				INNER JOIN Pro_Sec USING (sec_id)
				WHERE (sucursal.suc_id = $suc_id   and 
						sector.bod_id=bodega.bod_id and 
						Pro_Sec.sec_id=Sector.sec_id  );"						
						, $db_zapatos);
	
	
		mysql_query("DELETE FROM Bodega, Sucursal,Sector 
				USING Sucursal
				INNER JOIN Bodega  USING (suc_id)
				INNER JOIN Sector  USING (bod_id)
						
				WHERE (sucursal.suc_id = $suc_id   and 
						sector.bod_id=bodega.bod_id );"						
						, $db_zapatos);
			 
		mysql_query("DELETE FROM Bodega, Sucursal,Sector, Bod_Per 
				USING Sucursal
				INNER JOIN Bodega  USING (suc_id)
				INNER JOIN Sector  USING (bod_id)
				INNER JOIN Bod_Per USING (bod_id)
				WHERE (sucursal.suc_id = $suc_id   and 
						sector.bod_id=bodega.bod_id and 
						Bod_Per.bod_id=Bodega.bod_id);"												
						, $db_zapatos);
						
		mysql_query("DELETE FROM Bodega, Sucursal, Bod_Per 
				USING Sucursal
				INNER JOIN Bodega  USING (suc_id)
				INNER JOIN Bod_Per USING (bod_id)
				WHERE (sucursal.suc_id = $suc_id   and 
						Bod_Per.bod_id=Bodega.bod_id);"												
						, $db_zapatos);

			 
		mysql_query("DELETE FROM Sucursal WHERE suc_id=$suc_id;",$db_zapatos);

		mysql_query("DELETE FROM Bodega WHERE (suc_id = $suc_id  );"	, $db_zapatos);
		 
			 
			 
		 
		 

	
	

	
	/*
	else if(mysql_query("DELETE FROM Sucursal WHERE suc_id=$suc_id;",$db_zapatos)== TRUE){
	               $jsondata['respuesta'] =1;
/*		if(mysql_query("DELETE FROM Bodega, Sucursal,Sector, Pro_Sec, Bod_Per 
						USING Sucursal
						INNER JOIN Bodega  USING (suc_id)
						INNER JOIN Sector  USING (bod_id)
						INNER JOIN Pro_Sec USING (sec_id)
						INNER JOIN Bod_Per USING (bod_id)
						WHERE (sucursal.suc_id = $suc_id   and 
						       sector.bod_id=bodega.bod_id and 
							   Pro_Sec.sec_id=Sector.sec_id and 
							   Bod_Per.bod_id=Bodega.bod_id)
								
						
						;"
						
						, $db_zapatos) == TRUE){
		
				$jsondata['respuesta'] = 1;
			
				
			}
		else{
			$error['delete_h'] = "Error -- ocurrio un error al borrar la sucursal";
		}*/
		break;
	case 4: // SELECT
		$query = "SELECT * FROM Sucursal NATURAL LEFT JOIN Localidad NATURAL LEFT JOIN Region WHERE 1";
		
		if(isset($_POST['loc_id']) && $_POST['loc_id'] != 0) 
			$query .= " AND loc_id = ".$_POST['loc_id'];
		elseif(isset($_POST['reg_id']) && $_POST['reg_id'] != 0) 
			$query.= " AND reg_id = ".$_POST['reg_id'];

		$query .= " ORDER BY ".$_POST['orden']." ".$_POST['osentido']." LIMIT ".$_POST['inicio'].", ".$_POST['tam']." ";
		$query .= ";";  
		$resultado = mysql_query ($query, $db_zapatos);
		
		if( !$resultado) { 
			$error['query'] = 'Error al traer los datos desde la base.'; 
			break;
		}

		$jsondata['num_rows'] = mysql_num_rows($resultado);
		if( $jsondata['num_rows'] == 0) break;

		for($i = 0; $i < $jsondata['num_rows']; $i++) 
			$jsondata['rows'][$i] = mysql_fetch_assoc($resultado);
		break;
	default:
		$error['error'] = 'Error -- funcion desconocida.';
}

if( !empty( $error)) {
	$jsondata['error'] = 1;
	$jsondata['lista_errores'] = $error;
} else $jsondata['error'] = 0;
echo json_encode($jsondata);
?>